package com.mine.yyb.modules.sys.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Row;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mine.yyb.common.consts.YybConstants;
import com.mine.yyb.common.utils.exception.MineErrorCode;
import com.mine.yyb.common.utils.exception.MineException;
import com.mine.yyb.common.utils.execl.MineExcelReport;
import com.mine.yyb.common.utils.jdbc.MinePagination;
import com.mine.yyb.modules.sys.bo.OrderBO;
import com.mine.yyb.modules.sys.bo.UserBO;
import com.mine.yyb.modules.sys.dao.IOrderDao;
import com.mine.yyb.modules.sys.dao.IReportDao;
import com.mine.yyb.modules.sys.dao.IRyReportDao;
import com.mine.yyb.modules.sys.dao.IUserDao;
import com.mine.yyb.modules.sys.enums.OrderRebateEnum;
import com.mine.yyb.modules.sys.enums.OrderValidEnum;
import com.mine.yyb.modules.sys.enums.ShopSiteEnum;
import com.mine.yyb.modules.sys.service.IOrderService;
import com.mine.yyb.modules.sys.vo.OrderVO;
import com.mine.yyb.modules.sys.vo.ReportVO;
import com.mine.yyb.modules.sys.vo.RyReportVO;
import com.mine.yyb.modules.sys.vo.UserVO;

@Service(value = "iOrderService")
public class OrderServiceImpl implements IOrderService
{
    // 日期格式化
    public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    
    private static Logger logger = LoggerFactory.getLogger(OrderServiceImpl.class);
    
    @Autowired
    private IOrderDao iOrderDao;
    
    @Autowired
    private IReportDao iReportDao;
    
    @Autowired
    private IRyReportDao iRyReportDao;
    
    @Autowired
    private IUserDao iUserDao;
    
    // 时间误差
    // private static long offset = 24 * 60 * 60;
    
    @Override
    public String saveOrder(OrderBO orderBO)
        throws MineException
    {
        try
        {
            logger.info(orderBO.toString());
            
            OrderVO orderVO = new OrderVO();
            orderVO.setInnerID(orderBO.getInnerID());
            orderVO.setParentInnerID(orderBO.getParentInnerID());
            String rebateID = UUID.randomUUID().toString().replaceAll("-", "");
            orderVO.setRebateID(rebateID);
            orderVO.setRebate(0);
            orderVO.setOrderID(orderBO.getOrderID());
            orderVO.setShopSite(orderBO.getShopSite());
            orderVO.setOrderPrice(orderBO.getOrderPrice());
            orderVO.setIsValid(OrderValidEnum.ORDER_IS_VERIFY.getStatus());
            orderVO.setIsRebate(OrderRebateEnum.ORDER_IS_NOT_REBATE.getStatus());
            
            orderVO.setOrderDate(orderBO.getOrderDate());
            if(StringUtils.isNotBlank(orderBO.getLabel())) 
            {
                String[] arr = orderBO.getLabel().split(",");
                for (String label : arr)
                {
                    orderVO.setLabel(label);
                    iOrderDao.save(orderVO);
                }
            }
            
            return MineErrorCode.SUCCESS.name();
        }
        catch (MineException e)
        {
            throw e;
        }
        catch (Exception e)
        {
            throw new MineException(MineErrorCode.ERROR_INTERNAL, e);
        }
    }
    
    public static void main(String[] args)
        throws ParseException
    {
        // Date date = new Date();
        //
        // SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY-MM-dd");
        // System.out.println(simpleDateFormat.format(date));
        
        String str = "C03-3230490-4906441";
        
        str = StringUtils.substring(str, 0, str.length() - 1);
        
        System.out.println(str);
        
    }
    
    @Override
    public String updateOrder(OrderBO orderBO)
        throws MineException
    {
        try
        {
            logger.info(orderBO.toString());
            
            OrderVO orderVO = new OrderVO();
            orderVO.setOrderID(orderBO.getOrderID());
            orderVO.setInnerID(orderBO.getInnerID());
            orderVO.setOrderPrice(orderBO.getOrderPrice());
            orderVO.setRebate(orderBO.getRebate());
            orderVO.setParentRebate(orderBO.getParentRebate());
            orderVO.setOrderDate(orderBO.getOrderDate());
            orderVO.setIsRebate(orderBO.getIsRebate());
            orderVO.setIsParentRebate(orderBO.getIsParentRebate());
            orderVO.setIsValid(orderBO.getIsValid());
            
            int result = iOrderDao.update(orderVO);
            return result > 0 ? MineErrorCode.SUCCESS.name() : MineErrorCode.ERROR_INTERNAL.name();
            
        }
        catch (MineException e)
        {
            throw e;
        }
        catch (Exception e)
        {
            throw new MineException(MineErrorCode.ERROR_INTERNAL, e);
        }
    }
    
    @Override
    public List<OrderVO> queryOrderList(UserBO userBO)
        throws MineException
    {
        try
        {
            logger.info(userBO.toString());
            
            Map<String, Object> paramsMap = new HashMap<String, Object>();
            paramsMap.put("orderID", userBO.getOrderID());
            paramsMap.put("innerID", userBO.getInnerID());
            
            return iOrderDao.queryOrderList(paramsMap);
        }
        catch (MineException e)
        {
            throw e;
        }
        catch (Exception e)
        {
            throw new MineException(MineErrorCode.ERROR_INTERNAL, e);
        }
    }
    
    @Override
    public MinePagination<OrderVO> queryOrderList(OrderBO orderBO)
        throws MineException
    {
        try
        {
            logger.info(orderBO.toString());
            
            OrderVO orderVO = new OrderVO();
            orderVO.setInnerID(orderBO.getInnerID());
            orderVO.setOrderID(orderBO.getOrderID());
            orderVO.setShopSite(ShopSiteEnum.NEW_AMAZON_JP.getSite());
            orderVO.setIsRebate(orderBO.getIsRebate());
            orderVO.setIsValid(orderBO.getIsValid());
            
            return iOrderDao.queryOrderList(orderVO,
                orderBO.getsEcho(),
                orderBO.getiDisplayStart(),
                orderBO.getiDisplayLength(),
                orderBO.getStartDate(),
                orderBO.getEndDate());
        }
        catch (MineException e)
        {
            throw e;
        }
        catch (Exception e)
        {
            throw new MineException(MineErrorCode.ERROR_INTERNAL, e);
        }
    }
    
    @Override
    public MinePagination<OrderVO> queryQtOrderList(OrderBO orderBO)
        throws MineException
    {
        try
        {
            logger.info(orderBO.toString());
            
            OrderVO orderVO = new OrderVO();
            orderVO.setInnerID(orderBO.getInnerID());
            orderVO.setOrderID(orderBO.getOrderID());
            orderVO.setShopSite(orderBO.getShopSite());
            orderVO.setIsRebate(orderBO.getIsRebate());
            orderVO.setIsValid(orderBO.getIsValid());
            
            return iOrderDao.queryQtOrderList(orderVO,
                orderBO.getsEcho(),
                orderBO.getiDisplayStart(),
                orderBO.getiDisplayLength(),
                orderBO.getStartDate(),
                orderBO.getEndDate());
        }
        catch (MineException e)
        {
            throw e;
        }
        catch (Exception e)
        {
            throw new MineException(MineErrorCode.ERROR_INTERNAL, e);
        }
    }
    
    @Override
    public MinePagination<OrderVO> queryOrderListTo(OrderBO orderBO)
        throws MineException
    {
        try
        {
            logger.info(orderBO.toString());
            
            OrderVO orderVO = new OrderVO();
            orderVO.setUsername(orderBO.getUsername());
            orderVO.setInnerID(orderBO.getInnerID());
            orderVO.setOrderID(orderBO.getOrderID());
            orderVO.setShopSite(orderBO.getShopSite());
            orderVO.setIsRebate(orderBO.getIsRebate());
            orderVO.setIsValid(orderBO.getIsValid());
            
            return iOrderDao.queryOrderListTo(orderVO,
                orderBO.getsEcho(),
                orderBO.getiDisplayStart(),
                orderBO.getiDisplayLength(),
                orderBO.getStartDate(),
                orderBO.getEndDate());
        }
        catch (MineException e)
        {
            throw e;
        }
        catch (Exception e)
        {
            throw new MineException(MineErrorCode.ERROR_INTERNAL, e);
        }
    }
    
    @Override
    public MinePagination<OrderVO> queryChildRebateList(OrderBO orderBO)
        throws MineException
    {
        try
        {
            logger.info(orderBO.toString());
            
            OrderVO orderVO = new OrderVO();
            orderVO.setUsername(orderBO.getUsername());
            orderVO.setInnerID(orderBO.getInnerID());
            orderVO.setOrderID(orderBO.getOrderID());
            orderVO.setShopSite(orderBO.getShopSite());
            orderVO.setIsParentRebate(orderBO.getIsParentRebate());
            
            return iOrderDao.queryChildRebateList(orderVO,
                orderBO.getsEcho(),
                orderBO.getiDisplayStart(),
                orderBO.getiDisplayLength(),
                orderBO.getStartDate(),
                orderBO.getEndDate());
        }
        catch (MineException e)
        {
            throw e;
        }
        catch (Exception e)
        {
            throw new MineException(MineErrorCode.ERROR_INTERNAL, e);
        }
    }
    
    @Override
    public boolean isOrderExist(OrderBO orderBO)
        throws MineException
    {
        try
        {
            logger.info(orderBO.toString());
            
            Map<String, Object> paramsMap = new HashMap<String, Object>();
            paramsMap.put("orderID", orderBO.getOrderID());
            
            List<OrderVO> temp = iOrderDao.queryOrderList(paramsMap);
            
            if (temp == null || temp.isEmpty())
            {
                return false;
            }
            return true;
        }
        catch (MineException e)
        {
            throw e;
        }
        catch (Exception e)
        {
            throw new MineException(MineErrorCode.ERROR_INTERNAL, e);
        }
    }
    
    @Override
    public List<OrderVO> compareOrderCorrect()
        throws MineException
    {
        try
        {
            logger.info("compareOrder begin");
            
            Map<String, Object> paramsMap = new HashMap<String, Object>();
            paramsMap.put("isValid", OrderValidEnum.ORDER_IS_VERIFY.getStatus());
            String lastDate = getLastFourMonthDate();
            paramsMap.put("lastDate", lastDate);
            paramsMap.put("shopSite", "2,3,4");
            List<OrderVO> orderList = iOrderDao.queryOrderList(paramsMap);
            List<UserVO> childUserList = iUserDao.queryChildList(new UserVO());
            // 存储用户上下线关系
            Map<String, UserVO> childUserMap = new HashMap<String, UserVO>();
            // 将上下线用户转成map存储，便于检索
            for (UserVO userVO : childUserList)
            {
                String innerID = userVO.getInnerID();
                childUserMap.put(innerID, userVO);
            }
            for (OrderVO orderVO : orderList)
            {
                String innerID = orderVO.getInnerID();// 当前订单的用户的内部ID
                if (childUserMap.containsKey(innerID)) // 如果当前的用户内部ID存在上下线关系
                {
                    UserVO userVO = childUserMap.get(innerID);
                    if (StringUtils.isNotBlank(userVO.getParentInnerID()))// 当前用户有上线用户
                    {
                        UserVO parentUserVO = childUserMap.get(userVO.getParentInnerID());
                        compare(orderVO, parentUserVO, userVO);
                    }
                    else
                    {
                        compare(orderVO, null, null);
                    }
                }
                else
                {
                    compare(orderVO, null, null);
                }
            }
            return orderList;
        }
        catch (MineException e)
        {
            throw e;
        }
        catch (Exception e)
        {
            throw new MineException(MineErrorCode.ERROR_INTERNAL, e);
        }
    }
    
    /**
     * <pre>
     * <一句话功能简述>
     * 
     * 对比订单信息与日亚反馈报表
     * 
     * <功能详细描述>
     * </pre>
     *
     * @author xuganhua
     * @version [版本号, 2016年1月9日]
     * @param orderVO
     * @param parentUserVO 上线用户
     * @param userVO 订单所属用户
     * @return
     * @throws Exception
     * @see [类、类#方法、类#成员]
     */
    private OrderVO compare(OrderVO orderVO, UserVO parentUserVO, UserVO userVO)
        throws Exception
    {
        if (StringUtils.isBlank(orderVO.getLabel()))
        {
            orderVO.setIsValid(OrderValidEnum.ORDER_IS_VERIFY.getStatus());
        }
        else
        {
            ReportVO reportVO = new ReportVO();
            reportVO.setLabel(orderVO.getLabel());
            reportVO.setLabelTo(orderVO.getInnerID());
            reportVO = iReportDao.queryReport(reportVO);
            
            if (reportVO == null)
            {
                orderVO.setIsValid(OrderValidEnum.ORDER_IS_VERIFY.getStatus());
            }
            else
            {
                if (compareInnerID(orderVO, reportVO))
                {
                    double rebate = 0;
                    if (ShopSiteEnum.AMAZON_JP.getSite().equals(orderVO.getShopSite()))
                    {
                        rebate = reportVO.getRebate() * OrderBO.CJ_REBATE_RATE;
                    }
                    else if (ShopSiteEnum.LPM_COM.getSite().equals(orderVO.getShopSite()))
                    {
                        rebate = reportVO.getRebate() * OrderBO.CA_REBATE_RATE;
                    }
                    else if (ShopSiteEnum.JPLT_COM.getSite().equals(orderVO.getShopSite()))
                    {
                        rebate = reportVO.getRebate() * OrderBO.CLT_REBATE_RATE;
                    }
                    else if (ShopSiteEnum.LTGJ_COM.getSite().equals(orderVO.getShopSite()))
                    {
                        rebate = reportVO.getRebate() * OrderBO.CLTGJ_REBATE_RATE;
                    }
                    else if (ShopSiteEnum.AMAZON_CN.getSite().equals(orderVO.getShopSite()))
                    {
                        rebate = reportVO.getRebate() * OrderBO.CN_REBATE_RATE;
                    }
                    orderVO.setRebate(rebate);
                    if (parentUserVO != null && userVO != null)
                    {
                        if (checkOrderDateOverdue(orderVO.getOrderDate(), userVO.getCreateDate()) == false)
                        {
                            orderVO.setParentRebate(rebate * OrderBO.PARENT_REBATE_RATE);
                            orderVO.setParentInnerID(parentUserVO.getParentInnerID());
                        }
                    }
                    orderVO.setIsValid(OrderValidEnum.ORDER_IS_VALID.getStatus());
                }
                else
                {
                    orderVO.setIsValid(OrderValidEnum.ORDER_IS_VERIFY.getStatus());
                }
            }
        }
        orderVO.setOrderDate(null);
        orderVO.setInnerID(null);
        iOrderDao.update(orderVO);
        return orderVO;
    }
    
    @Override
    public List<OrderVO> compareCnamzOrderCorrect()
        throws MineException
    {
        try
        {
            logger.info("compareCnamzOrder begin");
            
            Map<String, Object> paramsMap = new HashMap<String, Object>();
            paramsMap.put("isValid", OrderValidEnum.ORDER_IS_VERIFY.getStatus());
            String lastDate = getLastFourMonthDate();
            paramsMap.put("lastDate", lastDate);
            List<OrderVO> orderList = iOrderDao.queryOrderList(paramsMap);
            List<UserVO> childUserList = iUserDao.queryChildList(new UserVO());
            // 存储用户上下线关系
            Map<String, UserVO> childUserMap = new HashMap<String, UserVO>();
            // 将上下线用户转成map存储，便于检索
            for (UserVO userVO : childUserList)
            {
                String innerID = userVO.getInnerID();
                childUserMap.put(innerID, userVO);
            }
            for (OrderVO orderVO : orderList)
            {
                // 仅比对中亚订单
                if (ShopSiteEnum.AMAZON_CN.getSite().equals(orderVO.getShopSite()))
                {
                    String innerID = orderVO.getInnerID();// 当前订单的用户的内部ID
                    if (childUserMap.containsKey(innerID)) // 如果当前的用户内部ID存在上下线关系
                    {
                        UserVO userVO = childUserMap.get(innerID);
                        if (StringUtils.isNotBlank(userVO.getParentInnerID()))// 当前用户有上线用户
                        {
                            UserVO parentUserVO = childUserMap.get(userVO.getParentInnerID());
                            compareCnamzOrder(orderVO, parentUserVO, userVO);
                        }
                        else
                        {
                            compareCnamzOrder(orderVO, null, null);
                        }
                    }
                    else
                    {
                        compareCnamzOrder(orderVO, null, null);
                    }
                }
            }
            return orderList;
        }
        catch (MineException e)
        {
            throw e;
        }
        catch (Exception e)
        {
            throw new MineException(MineErrorCode.ERROR_INTERNAL, e);
        }
    }
    
    @Override
    public List<RyReportVO> compareJpamzOrderCorrect()
        throws MineException
    {
        try
        {
            logger.info("compareJpamzOrder begin");
            
            Map<String, Object> paramsMap = new HashMap<String, Object>();
            String lastDate = getLastFourMonthDate();
            paramsMap.put("lastDate", lastDate);
            List<RyReportVO> reportList = iRyReportDao.queryRyReportList(paramsMap);
            List<UserVO> childUserList = iUserDao.queryChildList(new UserVO());
            // 存储用户上下线关系
            Map<String, UserVO> childUserMap = new HashMap<String, UserVO>();
            // 将上下线用户转成map存储，便于检索
            for (UserVO userVO : childUserList)
            {
                String innerID = userVO.getInnerID();
                childUserMap.put(innerID, userVO);
            }
            for (RyReportVO reportVO : reportList)
            {
                String innerID = reportVO.getLabelTo();// 当前订单的用户的内部ID
                if (childUserMap.containsKey(innerID)) // 如果当前的用户内部ID存在上下线关系
                {
                    UserVO userVO = childUserMap.get(innerID);
                    if (StringUtils.isNotBlank(userVO.getParentInnerID()))// 当前用户有上线用户
                    {
                        UserVO parentUserVO = childUserMap.get(userVO.getParentInnerID());
                        compareJpamzOrder(reportVO, parentUserVO, userVO);
                    }
                    else
                    {
                        compareJpamzOrder(reportVO, null, null);
                    }
                }
                else
                {
                    compareJpamzOrder(reportVO, null, null);
                }
            }
            return reportList;
        }
        catch (MineException e)
        {
            throw e;
        }
        catch (Exception e)
        {
            throw new MineException(MineErrorCode.ERROR_INTERNAL, e);
        }
    }
    
    /**
     * <pre>
     * <一句话功能简述>
     * 
     * 对比订单信息与日亚反馈报表
     * 
     * <功能详细描述>
     * </pre>
     *
     * @author xuganhua
     * @version [版本号, 2016年1月9日]
     * @param orderVO
     * @param parentUserVO 上线用户
     * @param userVO 订单所属用户
     * @return
     * @throws Exception
     * @see [类、类#方法、类#成员]
     */
    private OrderVO compareCnamzOrder(OrderVO orderVO, UserVO parentUserVO, UserVO userVO)
        throws Exception
    {
        ReportVO reportVO = new ReportVO();
        reportVO.setOrderDate(orderVO.getOrderDate());
        reportVO.setOrderID(StringUtils.substring(orderVO.getOrderID(), 0, orderVO.getOrderID().length() - 1) + "*");
        reportVO = iReportDao.queryReport(reportVO);
        
        if (reportVO == null)
        {
            orderVO.setIsValid(OrderValidEnum.ORDER_IS_VERIFY.getStatus());
        }
        else
        {
            double rebate = 0;
            rebate = reportVO.getOrderPrice() * OrderBO.CN_REBATE_RATE;
            orderVO.setRebate(rebate);
            if (parentUserVO != null && userVO != null)
            {
                if (checkOrderDateOverdue(orderVO.getOrderDate(), userVO.getCreateDate()) == false)
                {
                    orderVO.setParentRebate(rebate * OrderBO.PARENT_REBATE_RATE);
                    orderVO.setParentInnerID(parentUserVO.getInnerID());
                }
            }
            orderVO.setIsValid(OrderValidEnum.ORDER_IS_VALID.getStatus());
        }
        
        orderVO.setOrderDate(null);
        orderVO.setInnerID(null);
        iOrderDao.update(orderVO);
        return orderVO;
    }
    
    private OrderVO compareJpamzOrder(RyReportVO ryReportVO, UserVO parentUserVO, UserVO userVO)
        throws Exception
    {
        Map<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("orderID", ryReportVO.getOrderID());
        List<OrderVO> list = iOrderDao.queryOrderList(paramsMap);
        
        OrderVO orderVO = new OrderVO();
        orderVO.setOrderID(ryReportVO.getOrderID());
        double rebate = ryReportVO.getRebate() * ryReportVO.getCount() * OrderBO.CJ_REBATE_RATE;
        orderVO.setRebate(rebate);
        
        if (parentUserVO != null && userVO != null)
        {
            if (checkOrderDateOverdue(ryReportVO.getOrderDate(), userVO.getCreateDate()) == false)
            {
                orderVO.setParentRebate(rebate * OrderBO.PARENT_REBATE_RATE);
                orderVO.setParentInnerID(parentUserVO.getInnerID());
            }
        }
        orderVO.setIsValid(OrderValidEnum.ORDER_IS_VALID.getStatus());
        orderVO.setOrderPrice(ryReportVO.getPrice() * ryReportVO.getCount());
        orderVO.setActivity(ryReportVO.getActivity());
        orderVO.setProductName(ryReportVO.getProductName());
        orderVO.setPrice(ryReportVO.getPrice());
        orderVO.setCount(ryReportVO.getCount());
        
        if (list.size() > 0)
        {
            orderVO.setOrderDate(null);
            orderVO.setInnerID(null);
            orderVO.setIsRebate(OrderRebateEnum.ORDER_IS_NOT_REBATE.getStatus());
            iOrderDao.update(orderVO);
        }
        else
        {
            String rebateID = UUID.randomUUID().toString().replaceAll("-", "");
            orderVO.setShopSite(ShopSiteEnum.NEW_AMAZON_JP.getSite());
            orderVO.setRebateID(rebateID);
            orderVO.setInnerID(ryReportVO.getLabelTo());
            orderVO.setOrderDate(ryReportVO.getOrderDate());
            orderVO.setIsRebate(OrderRebateEnum.ORDER_IS_NOT_REBATE.getStatus());
            iOrderDao.save(orderVO);
        }
        return orderVO;
    }
    
    /**
     * <pre>
     * <一句话功能简述>
     * 
     * 检查订单日期是否超过了最后的分成日期
     * 
     * false 表示没有超过
     * true 表示已经超过
     * 
     * <功能详细描述>
     * </pre>
     *
     * @author xuganhua
     * @version [版本号, 2016年3月12日]
     * @param orderDate 表示订单日期
     * @param createDate 表示用户的创建日期
     * @return
     * @throws ParseException
     * @see [类、类#方法、类#成员]
     */
    private boolean checkOrderDateOverdue(String orderDate, String createDate)
        throws ParseException
    {
        Calendar createDateCalendar = Calendar.getInstance();
        createDateCalendar.setTime(sdf.parse(createDate));
        
        Calendar orderDateCalendar = Calendar.getInstance();
        orderDateCalendar.setTime(sdf.parse(orderDate));
        
        Calendar deadline = Calendar.getInstance();
        deadline.set(Calendar.YEAR, createDateCalendar.get(Calendar.YEAR));// 年
        deadline.set(Calendar.MONTH, createDateCalendar.get(Calendar.MONTH));// 月
        deadline.set(Calendar.DAY_OF_MONTH, createDateCalendar.get(Calendar.DAY_OF_MONTH));// 日
        
        deadline.add(Calendar.MONTH, OrderBO.PARENT_REBATE_MONTH);// 加一年
        deadline.add(Calendar.DAY_OF_MONTH, -1);// 减一天
        if (orderDateCalendar.before(deadline))
        {
            return false;
        }
        else
        {
            return true;
        }
    }
    
    /**
     * <pre>
     * <一句话功能简述>
     * 
     * 获得当前日期前四个月的那天日期
     * 
     * <功能详细描述>
     * </pre>
     *
     * @author 徐干华 0118
     * @version [版本号, 2017年3月4日]
     * @return
     * @see [类、类#方法、类#成员]
     */
    private String getLastFourMonthDate()
        throws ParseException
    {
        Date currentDate = new Date();
        String currentDateStr = sdf.format(currentDate);
        
        Calendar deadline = Calendar.getInstance();
        deadline.setTime(sdf.parse(currentDateStr));
        deadline.add(Calendar.DAY_OF_MONTH, -120);// 减一天
        
        return sdf.format(deadline.getTime());
    }
    
    /**
     * <pre>
     * <一句话功能简述>
     * 
     * 比较订单的日期
     * 
     * <功能详细描述>
     * </pre>
     *
     * @author xuganhua
     * @version [版本号, 2016年1月9日]
     * @param orderVO 用户登记的订单信息
     * @param reportVO 日亚反馈的订单信息
     * @return
     * @throws MineException
     * @see [类、类#方法、类#成员]
     */
    // private boolean compareOrderDate(OrderVO orderVO, ReportVO reportVO)
    // throws Exception
    // {
    // boolean flag = false;
    // String orderDate = orderVO.getOrderDate();// 用户登记的订单日期
    // String report_orderDate = reportVO.getOrderDate();
    //
    // SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    // Date date = simpleDateFormat.parse(orderDate);
    // long orderDate_timeStemp = date.getTime() / 1000;
    //
    // date = simpleDateFormat.parse(report_orderDate);
    // long report_orderDate_timeStemp = date.getTime() / 1000;
    //
    // if (Math.abs(report_orderDate_timeStemp - orderDate_timeStemp) <= offset)
    // {
    // flag = true;
    // }
    // return flag;
    // }
    
    /**
     * <pre>
     * <一句话功能简述>
     * 
     * 比较用户的内部ID是否与日亚反馈的一致
     * 
     * <功能详细描述>
     * </pre>
     *
     * @author xuganhua
     * @version [版本号, 2016年1月9日]
     * @param orderVO
     * @param reportVO
     * @return
     * @throws Exception
     * @see [类、类#方法、类#成员]
     */
    private boolean compareInnerID(OrderVO orderVO, ReportVO reportVO)
        throws Exception
    {
        boolean flag = false;
        String innerID = orderVO.getInnerID();// 用户的内部ID
        String labelTo = reportVO.getLabelTo();// 日亚反馈的标签2
        
        if (innerID.equals(labelTo))
        {
            flag = true;
        }
        return flag;
    }
    
    /**
     * <pre>
     * <一句话功能简述>
     * 
     * 比较用户的登记订单价格与日亚反馈的订单价格是否符合标准
     * 
     * <功能详细描述>
     * </pre>
     *
     * @author xuganhua
     * @version [版本号, 2016年1月9日]
     * @param orderVO
     * @param reportVO
     * @return
     * @throws Exception
     * @see [类、类#方法、类#成员]
     */
    // private boolean compareOrderPrice(OrderVO orderVO, ReportVO reportVO)
    // throws Exception
    // {
    // boolean flag = false;
    //
    // double orderPrice = orderVO.getOrderPrice();
    // double report_orderPrice = reportVO.getOrderPrice();
    // report_orderPrice = report_orderPrice * OrderBO.CJ_RATE;
    //
    // if (Math.abs(report_orderPrice - orderPrice) <= report_orderPrice * 0.1)
    // {
    // flag = true;
    // }
    // return flag;
    // }
    
    @Override
    public OrderVO querySum(OrderBO orderBO)
        throws MineException
    {
        try
        {
            logger.info(orderBO.toString());
            
            Map<String, Object> paramsMap = new HashMap<String, Object>();
            paramsMap.put("innerID", orderBO.getInnerID());
            paramsMap.put("orderID", orderBO.getOrderID());
            paramsMap.put("shopSite", ShopSiteEnum.NEW_AMAZON_JP.getSite());
            paramsMap.put("isRebate", orderBO.getIsRebate());
            paramsMap.put("isValid", orderBO.getIsValid());
            
            OrderVO orderVO = iOrderDao.querySum(paramsMap, orderBO.getStartDate(), orderBO.getEndDate());
            if (orderVO == null)
            {
                return new OrderVO();
            }
            else
            {
                return orderVO;
            }
        }
        catch (MineException e)
        {
            throw e;
        }
        catch (Exception e)
        {
            throw new MineException(MineErrorCode.ERROR_INTERNAL, e);
        }
    }
    
    @Override
    public OrderVO queryQtSum(OrderBO orderBO)
        throws MineException
    {
        try
        {
            logger.info(orderBO.toString());
            
            Map<String, Object> paramsMap = new HashMap<String, Object>();
            paramsMap.put("innerID", orderBO.getInnerID());
            paramsMap.put("orderID", orderBO.getOrderID());
            paramsMap.put("shopSite", orderBO.getShopSite());
            paramsMap.put("isRebate", orderBO.getIsRebate());
            paramsMap.put("isValid", orderBO.getIsValid());
            
            OrderVO orderVO = iOrderDao.queryQtSum(paramsMap, orderBO.getStartDate(), orderBO.getEndDate());
            if (orderVO == null)
            {
                return new OrderVO();
            }
            else
            {
                return orderVO;
            }
        }
        catch (MineException e)
        {
            throw e;
        }
        catch (Exception e)
        {
            throw new MineException(MineErrorCode.ERROR_INTERNAL, e);
        }
    }
    
    @Override
    public OrderVO queryAllSum(OrderBO orderBO)
        throws MineException
    {
        try
        {
            logger.info(orderBO.toString());
            
            Map<String, Object> paramsMap = new HashMap<String, Object>();
            paramsMap.put("username", orderBO.getUsername());
            paramsMap.put("innerID", orderBO.getInnerID());
            paramsMap.put("orderID", orderBO.getOrderID());
            paramsMap.put("shopSite", orderBO.getShopSite());
            paramsMap.put("isRebate", orderBO.getIsRebate());
            paramsMap.put("isValid", orderBO.getIsValid());
            
            OrderVO orderVO = iOrderDao.queryAllSum(paramsMap, orderBO.getStartDate(), orderBO.getEndDate());
            if (orderVO == null)
            {
                return new OrderVO();
            }
            else
            {
                return orderVO;
            }
        }
        catch (MineException e)
        {
            throw e;
        }
        catch (Exception e)
        {
            throw new MineException(MineErrorCode.ERROR_INTERNAL, e);
        }
        
    }
    
    @Override
    public OrderVO queryAllSumChildRebate(OrderBO orderBO)
        throws MineException
    {
        try
        {
            logger.info(orderBO.toString());
            
            Map<String, Object> paramsMap = new HashMap<String, Object>();
            paramsMap.put("username", orderBO.getUsername());
            paramsMap.put("innerID", orderBO.getInnerID());
            paramsMap.put("orderID", orderBO.getOrderID());
            paramsMap.put("shopSite", orderBO.getShopSite());
            paramsMap.put("isParentRebate", orderBO.getIsParentRebate());
            
            OrderVO orderVO = iOrderDao.queryAllSumChildRebate(paramsMap, orderBO.getStartDate(), orderBO.getEndDate());
            if (orderVO == null)
            {
                return new OrderVO();
            }
            else
            {
                return orderVO;
            }
        }
        catch (MineException e)
        {
            throw e;
        }
        catch (Exception e)
        {
            throw new MineException(MineErrorCode.ERROR_INTERNAL, e);
        }
    }
    
    @Override
    public MinePagination<Map<String, String>> queryRebate(OrderBO orderBO)
        throws MineException
    {
        try
        {
            logger.info(orderBO.toString());
            
            Map<String, Object> paramsMap = new HashMap<String, Object>();
            paramsMap.put("innerID", orderBO.getInnerID());
            paramsMap.put("orderYear", orderBO.getOrderYear());
            paramsMap.put("orderMonth", orderBO.getOrderMonth());
            
            return iOrderDao.queryRebate(paramsMap,
                orderBO.getsEcho(),
                orderBO.getiDisplayStart(),
                orderBO.getiDisplayLength());
            
        }
        catch (MineException e)
        {
            throw e;
        }
        catch (Exception e)
        {
            throw new MineException(MineErrorCode.ERROR_INTERNAL, e);
        }
    }
    
    @Override
    public MinePagination<Map<String, String>> queryChildRebate(OrderBO orderBO)
        throws MineException
    {
        try
        {
            logger.info(orderBO.toString());
            
            Map<String, Object> paramsMap = new HashMap<String, Object>();
            paramsMap.put("parentInnerID", orderBO.getInnerID());
            paramsMap.put("orderYear", orderBO.getOrderYear());
            paramsMap.put("orderMonth", orderBO.getOrderMonth());
            
            return iOrderDao.queryChildRebate(paramsMap,
                orderBO.getsEcho(),
                orderBO.getiDisplayStart(),
                orderBO.getiDisplayLength());
        }
        catch (MineException e)
        {
            throw e;
        }
        catch (Exception e)
        {
            throw new MineException(MineErrorCode.ERROR_INTERNAL, e);
        }
    }
    
    @Override
    public MinePagination<Map<String, String>> queryAllRebate(OrderBO orderBO)
        throws MineException
    {
        try
        {
            logger.info(orderBO.toString());
            
            Map<String, Object> paramsMap = new HashMap<String, Object>();
            paramsMap.put("username", orderBO.getUsername());
            paramsMap.put("orderYear", orderBO.getOrderYear());
            paramsMap.put("orderMonth", orderBO.getOrderMonth());
            
            return iOrderDao.queryAllRebate(paramsMap,
                orderBO.getsEcho(),
                orderBO.getiDisplayStart(),
                orderBO.getiDisplayLength());
        }
        catch (MineException e)
        {
            throw e;
        }
        catch (Exception e)
        {
            throw new MineException(MineErrorCode.ERROR_INTERNAL, e);
        }
    }
    
    @Override
    public MinePagination<Map<String, String>> queryAllChildRebate(OrderBO orderBO)
        throws MineException
    {
        try
        {
            logger.info(orderBO.toString());
            
            Map<String, Object> paramsMap = new HashMap<String, Object>();
            paramsMap.put("username", orderBO.getUsername());
            paramsMap.put("orderYear", orderBO.getOrderYear());
            paramsMap.put("orderMonth", orderBO.getOrderMonth());
            
            return iOrderDao.queryAllChildRebate(paramsMap,
                orderBO.getsEcho(),
                orderBO.getiDisplayStart(),
                orderBO.getiDisplayLength());
        }
        catch (MineException e)
        {
            throw e;
        }
        catch (Exception e)
        {
            throw new MineException(MineErrorCode.ERROR_INTERNAL, e);
        }
    }
    
    @Override
    public MineExcelReport exportOrderData(OrderBO orderBO)
        throws MineException
    {
        try
        {
            logger.info(orderBO.toString());
            OrderVO orderVO = new OrderVO();
            orderVO.setInnerID(orderBO.getInnerID());
            orderVO.setOrderID(orderBO.getOrderID());
            orderVO.setShopSite(ShopSiteEnum.NEW_AMAZON_JP.getSite());
            orderVO.setIsRebate(orderBO.getIsRebate());
            orderVO.setIsValid(orderBO.getIsValid());
            List<OrderVO> list = iOrderDao.queryOrderList(orderVO, orderBO.getStartDate(), orderBO.getEndDate());
            // 根据模版文件创建表格对象
            MineExcelReport excelReport = new MineExcelReport(YybConstants.RY_EXECL_TEMPLATE_PATH);
            // 存在数据则逐条添加数据到表格
            if (list != null && !list.isEmpty())
            {
                // 添加数据
                for (OrderVO temp : list)
                {
                    // 添加一行
                    Row row = excelReport.addRow();
                    // 添加单元格
                    excelReport.addCell(row, 0, temp.getOrderID());
                    excelReport.addCell(row, 1, temp.getActivity());
                    excelReport.addCell(row, 2, temp.getProductName());
                    excelReport.addCell(row, 3, temp.getPrice());
                    excelReport.addCell(row, 4, temp.getRebate());
                    excelReport.addCell(row, 5, temp.getCount());
                    excelReport.addCell(row, 6, temp.getOrderDate());
                    if (OrderValidEnum.ORDER_IS_VALID.getStatus() == temp.getIsValid())
                    {
                        excelReport.addCell(row, 7, "有效");
                    }
                    
                    if (OrderRebateEnum.ORDER_IS_REBATE.getStatus() == temp.getIsRebate())
                    {
                        excelReport.addCell(row, 8, "已返利");
                    }
                    else if (OrderRebateEnum.ORDER_IS_NOT_REBATE.getStatus() == temp.getIsRebate())
                    {
                        excelReport.addCell(row, 8, "未返利");
                    }
                }
            }
            return excelReport;
        }
        catch (MineException e)
        {
            throw e;
        }
        catch (Exception e)
        {
            throw new MineException(MineErrorCode.ERROR_INTERNAL, e);
        }
    }
    
    @Override
    public MineExcelReport exportQtOrderData(OrderBO orderBO)
        throws MineException
    {
        try
        {
            logger.info(orderBO.toString());
            OrderVO orderVO = new OrderVO();
            orderVO.setInnerID(orderBO.getInnerID());
            orderVO.setOrderID(orderBO.getOrderID());
            orderVO.setShopSite(orderBO.getShopSite());
            orderVO.setIsRebate(orderBO.getIsRebate());
            orderVO.setIsValid(orderBO.getIsValid());
            List<OrderVO> list = iOrderDao.queryQtOrderList(orderVO, orderBO.getStartDate(), orderBO.getEndDate());
            // 根据模版文件创建表格对象
            MineExcelReport excelReport = new MineExcelReport(YybConstants.NORMAL_USER_EXECL_TEMPLATE_PATH);
            // 存在数据则逐条添加数据到表格
            if (list != null && !list.isEmpty())
            {
                // 添加数据
                for (OrderVO temp : list)
                {
                    // 添加一行
                    Row row = excelReport.addRow();
                    // 添加单元格
                    excelReport.addCell(row, 0, temp.getOrderID());
                    if (ShopSiteEnum.AMAZON_JP.getSite().equals(temp.getShopSite()))
                    {
                        excelReport.addCell(row, 1, "日本亚马逊");
                    }
                    else if (ShopSiteEnum.LPM_COM.getSite().equals(temp.getShopSite()))
                    {
                        excelReport.addCell(row, 1, "6pm");
                    }
                    else if (ShopSiteEnum.JPLT_COM.getSite().equals(temp.getShopSite()))
                    {
                        excelReport.addCell(row, 1, "日本乐天");
                    }
                    else if (ShopSiteEnum.LTGJ_COM.getSite().equals(temp.getShopSite()))
                    {
                        excelReport.addCell(row, 1, "乐天国际");
                    }
                    else if (ShopSiteEnum.AMAZON_CN.getSite().equals(temp.getShopSite()))
                    {
                        excelReport.addCell(row, 1, "中国亚马逊");
                    }
                    excelReport.addCell(row, 2, temp.getRebate());
                    excelReport.addCell(row, 3, temp.getOrderPrice());
                    excelReport.addCell(row, 4, temp.getOrderDate());
                    if (OrderValidEnum.ORDER_IS_VALID.getStatus() == temp.getIsValid())
                    {
                        excelReport.addCell(row, 5, "有效");
                    }
                    else if (OrderValidEnum.ORDER_IS_INVALID.getStatus() == temp.getIsValid())
                    {
                        excelReport.addCell(row, 5, "无效");
                    }
                    else if (OrderValidEnum.ORDER_IS_VERIFY.getStatus() == temp.getIsValid())
                    {
                        excelReport.addCell(row, 5, "审核中");
                    }
                    
                    if (OrderRebateEnum.ORDER_IS_REBATE.getStatus() == temp.getIsRebate())
                    {
                        excelReport.addCell(row, 6, "已返利");
                    }
                    else if (OrderRebateEnum.ORDER_IS_NOT_REBATE.getStatus() == temp.getIsRebate())
                    {
                        excelReport.addCell(row, 6, "未返利");
                    }
                }
            }
            return excelReport;
        }
        catch (MineException e)
        {
            throw e;
        }
        catch (Exception e)
        {
            throw new MineException(MineErrorCode.ERROR_INTERNAL, e);
        }
    }
    
    @Override
    public MineExcelReport exportAllOrderData(OrderBO orderBO)
        throws MineException
    {
        try
        {
            logger.info(orderBO.toString());
            OrderVO orderVO = new OrderVO();
            orderVO.setInnerID(orderBO.getInnerID());
            orderVO.setOrderID(orderBO.getOrderID());
            orderVO.setShopSite(orderBO.getShopSite());
            orderVO.setIsRebate(orderBO.getIsRebate());
            orderVO.setIsValid(orderBO.getIsValid());
            orderVO.setUsername(orderBO.getUsername());
            
            List<OrderVO> list = iOrderDao.queryOrderListTo(orderVO, orderBO.getStartDate(), orderBO.getEndDate());
            // 根据模版文件创建表格对象
            MineExcelReport excelReport = new MineExcelReport(YybConstants.ADMIN_USER_EXECL_TEMPLATE_PATH);
            // 存在数据则逐条添加数据到表格
            if (list != null && !list.isEmpty())
            {
                // 添加数据
                for (OrderVO temp : list)
                {
                    // 添加一行
                    Row row = excelReport.addRow();
                    // 添加单元格
                    excelReport.addCell(row, 0, temp.getUsername());
                    excelReport.addCell(row, 1, temp.getOrderID());
                    if (ShopSiteEnum.AMAZON_JP.getSite().equals(temp.getShopSite()))
                    {
                        excelReport.addCell(row, 2, "日本亚马逊");
                    }
                    else if (ShopSiteEnum.LPM_COM.getSite().equals(temp.getShopSite()))
                    {
                        excelReport.addCell(row, 2, "6pm");
                    }
                    else if (ShopSiteEnum.JPLT_COM.getSite().equals(temp.getShopSite()))
                    {
                        excelReport.addCell(row, 2, "日本乐天");
                    }
                    else if (ShopSiteEnum.LTGJ_COM.getSite().equals(temp.getShopSite()))
                    {
                        excelReport.addCell(row, 2, "乐天国际");
                    }
                    else if (ShopSiteEnum.AMAZON_CN.getSite().equals(temp.getShopSite()))
                    {
                        excelReport.addCell(row, 2, "中国亚马逊");
                    }
                    else if (ShopSiteEnum.NEW_AMAZON_JP.getSite().equals(temp.getShopSite()))
                    {
                        excelReport.addCell(row, 2, "新版日本亚马逊");
                    }
                    excelReport.addCell(row, 3, temp.getRebate());
                    excelReport.addCell(row, 4, temp.getOrderPrice());
                    excelReport.addCell(row, 5, temp.getOrderDate());
                    if (OrderValidEnum.ORDER_IS_VALID.getStatus() == temp.getIsValid())
                    {
                        excelReport.addCell(row, 6, "有效");
                    }
                    else if (OrderValidEnum.ORDER_IS_INVALID.getStatus() == temp.getIsValid())
                    {
                        excelReport.addCell(row, 6, "无效");
                    }
                    else if (OrderValidEnum.ORDER_IS_VERIFY.getStatus() == temp.getIsValid())
                    {
                        excelReport.addCell(row, 6, "审核中");
                    }
                    
                    if (OrderRebateEnum.ORDER_IS_REBATE.getStatus() == temp.getIsRebate())
                    {
                        excelReport.addCell(row, 7, "已返利");
                    }
                    else if (OrderRebateEnum.ORDER_IS_NOT_REBATE.getStatus() == temp.getIsRebate())
                    {
                        excelReport.addCell(row, 7, "未返利");
                    }
                }
            }
            return excelReport;
        }
        catch (MineException e)
        {
            throw e;
        }
        catch (Exception e)
        {
            throw new MineException(MineErrorCode.ERROR_INTERNAL, e);
        }
    }
    
    @Override
    public MineExcelReport exportAllRebateData(OrderBO orderBO)
        throws MineException
    {
        try
        {
            logger.info(orderBO.toString());
            Map<String, Object> paramsMap = new HashMap<String, Object>();
            paramsMap.put("username", orderBO.getUsername());
            paramsMap.put("innerID", orderBO.getInnerID());
            paramsMap.put("orderYear", orderBO.getOrderYear());
            paramsMap.put("orderMonth", orderBO.getOrderMonth());
            
            List<Map<String, Object>> list = iOrderDao.queryAllRebate(paramsMap);
            // 根据模版文件创建表格对象
            MineExcelReport excelReport = new MineExcelReport(YybConstants.ADMIN_USER_REBATE_EXECL_TEMPLATE_PATH);
            // 存在数据则逐条添加数据到表格
            if (list != null && !list.isEmpty())
            {
                // 添加数据
                for (Map<String, Object> temp : list)
                {
                    // 添加一行
                    Row row = excelReport.addRow();
                    // 添加单元格
                    excelReport.addCell(row, 0, temp.get("username"));
                    excelReport.addCell(row, 1, temp.get("payAccount"));
                    excelReport.addCell(row, 2, temp.get("name"));
                    excelReport.addCell(row, 3, temp.get("orderDate"));
                    excelReport.addCell(row, 4, Double.valueOf(temp.get("rebate").toString()));
                    
                    int isRebate = Integer.parseInt(temp.get("isRebate").toString());
                    int count = Integer.parseInt(temp.get("count").toString());
                    if (count >= isRebate)
                    {
                        excelReport.addCell(row, 5, "未返利");
                    }
                    else if (count < isRebate)
                    {
                        excelReport.addCell(row, 5, "已返利");
                    }
                }
            }
            return excelReport;
        }
        catch (MineException e)
        {
            throw e;
        }
        catch (Exception e)
        {
            throw new MineException(MineErrorCode.ERROR_INTERNAL, e);
        }
    }
    
    @Override
    public MineExcelReport exportAllChildRebateData(OrderBO orderBO)
        throws MineException
    {
        try
        {
            logger.info(orderBO.toString());
            Map<String, Object> paramsMap = new HashMap<String, Object>();
            paramsMap.put("username", orderBO.getUsername());
            paramsMap.put("orderYear", orderBO.getOrderYear());
            paramsMap.put("orderMonth", orderBO.getOrderMonth());
            
            List<Map<String, Object>> list = iOrderDao.queryAllChildRebate(paramsMap);
            // 根据模版文件创建表格对象
            MineExcelReport excelReport = new MineExcelReport(YybConstants.ADMIN_USER_CHILD_REBATE_EXECL_TEMPLATE_PATH);
            // 存在数据则逐条添加数据到表格
            if (list != null && !list.isEmpty())
            {
                // 添加数据
                for (Map<String, Object> temp : list)
                {
                    // 添加一行
                    Row row = excelReport.addRow();
                    // 添加单元格
                    excelReport.addCell(row, 0, temp.get("parentUsername"));
                    excelReport.addCell(row, 1, temp.get("payAccount"));
                    excelReport.addCell(row, 2, temp.get("username"));
                    excelReport.addCell(row, 3, Double.valueOf(temp.get("parentRebate").toString()));
                    excelReport.addCell(row, 4, temp.get("orderDate"));
                    
                    int isParentRebate = Integer.parseInt(temp.get("isParentRebate").toString());
                    int count = Integer.parseInt(temp.get("count").toString());
                    if (count >= isParentRebate)
                    {
                        excelReport.addCell(row, 5, "未返利");
                    }
                    else if (count < isParentRebate)
                    {
                        excelReport.addCell(row, 5, "已返利");
                    }
                }
            }
            return excelReport;
        }
        catch (MineException e)
        {
            throw e;
        }
        catch (Exception e)
        {
            throw new MineException(MineErrorCode.ERROR_INTERNAL, e);
        }
    }
    
    /**
     * <pre>
     * <一句话功能简述>
     * <功能详细描述>
     * </pre>
     *
     * @author 姓名 工号
     * @version [版本号, 2017年7月28日]
     * @param orderVO
     * @throws MineException
     * @see [类、类#方法、类#成员]
     */
    @Override
    public void deleteOrder(OrderBO orderBO)
        throws MineException
    {
        try
        {
            logger.info(orderBO.toString());
            
            OrderVO orderVO = new OrderVO();
            orderVO.setOrderID(orderBO.getOrderID());
            
            iOrderDao.deleteOrder(orderVO);
        }
        catch (MineException e)
        {
            throw e;
        }
        catch (Exception e)
        {
            throw new MineException(MineErrorCode.ERROR_INTERNAL, e);
        }
        
    }
    
}
